package com.dq.repository.goods;


import com.dq.domain.goods.Goods;
import com.dq.domain.goods.Product;
import com.dq.domain.goods.centre.OrgGoodsCentre;
import com.dq.domain.sys.section.SysSection;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author cf
 */
@Repository
public interface OrgGoodsCentreRepository extends JpaRepository<OrgGoodsCentre, Long> {

    @Query(value = "SELECT SUM(og.count) FROM org_goods_centre og " +
            " WHERE og.deleted = :deleted" +
            " and (IF :orgId IS NOT NULL, org_id = :orgId, 1=1)" +
            " and (IF :goodsId IS NOT NULL, goods_id = :goodsId, 1=1)" +
            " and (IF :productId IS NOT NULL, product_id = :productId, 1=1)"
            , nativeQuery = true)
    Long stockCount(Boolean deleted, Long orgId, Long goodsId, Long productId);


    OrgGoodsCentre findByOrgAndProductAndDeletedFalse(SysSection section, Product product);
    List<OrgGoodsCentre> findByOrgAndGoodsAndDeletedFalse(SysSection section, Goods goods);
    List<OrgGoodsCentre> findByGoodsAndDeletedFalse(Goods goods);

}
